package com.chinasofti.info_manager.dao;

import com.chinasofti.info_manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao{

    private static ArrayList<Student> stus = new ArrayList<>();


    static{
        Student stu1 = new Student("0011","lilei","13","2010-01-02");
        Student stu2 = new Student("0012","hmm","12","2011-01-02");
        stus.add(stu1);
        stus.add(stu2);
    }

    //添加学生
    @Override
    public boolean addStudent(Student stu) {
        stus.add(stu);
        return true;
    }

    //查询全部学生
    @Override
    public Student[] findAllStudent() {
        Student[] students = new Student[stus.size()];
        for(int i=0;i<students.length;i++){
            students[i] = stus.get(i);
        }
        return students;
    }
    @Override
    public void deleteStudentById(String id) {
        int index = getIndex(id);//查询被删除id索引的位置
        stus.remove(index);//将索引位置使用null覆盖
    }
    @Override
    public void updateStudent(String updateId, Student newStu) {
        //找到要修改的索引位置
        int index = getIndex(updateId);
        stus.set(index,newStu);
    }

    //查询id是否存在 index = -1 表示id 不存在
    @Override
    public  int    getIndex(String id){
        int index = -1;
        for(int i=0;i<stus.size();i++){
            Student stu = stus.get(i);
            if(stu != null && stu.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }
}
